1

mrahmedcomputing

KS3, GCSE, A-Level Computing Resources

Lesson 1. Variables, IOs, Data Types


Lesson Objective

  • Learn how to display information on the screen and understand what a variable is.
  • Understand some basic Java syntax.

Lesson Notes

Variables and IOs

Every line of code that runs in Java must be inside a class. In the example above, we named the class Main. A class should always start with an uppercase first letter.

Code:

public class Main {
   public static void main(String args[]){
       System.out.println("Hello World, welcome to your first Java lesson.");
    }
}

Cmd Output:

Hello World, welcome to your first Java lesson.

The main() method is used in every Java program. Any code inside the main() method will be executed. Don't worry about the keywords before and after main. You will get to know them bit by bit while reading this tutorial. For now, just remember that every Java program has a class name which must match the filename, and that every program must contain the main() method.


System.out.println

To display information to the user, you must use the "System.out.println" method.

Code:

public class Hello_World_1 {
   public static void main(String args[]){
       System.out.println("Hello World, welcome to your first Java lesson.");
    }
}

Cmd Output:

Hello World, welcome to your first Java lesson.
I'm on the next line.

The "System.out.println" method is used to create new lines in the display.

Code:

public class Hello_World_1 {
   public static void main(String args[]){
       System.out.println("Hello World, welcome to your first Java lesson.");
       System.out.println("I'm on the next line.");
    }
}

Cmd Output:

Hello World, welcome to your first Java lesson.
I'm on the next line.

System.out.print

The "System.out.print" method allows you to stay on the same line in the display.

Code:

public class Hello_World_1 {
   public static void main(String args[]){
       System.out.print("Hello World.");
       System.out.print("I'm not... on the next line.");
    }
}

Cmd Output:

Hello World. I'm not... on the next line.

Variables

Variables are storage locations in your code that store data. They are assigned "tags" and are referred to throughout a program.

Code:

public class Hello_World_1 {
   public static void main(String args[]){
       System.out.println("Hello World.");
       String cat;
       cat = "Hello World again...";
       System.out.println(cat);
    }
}

Cmd Output:

Hello World
Hello World again...

Input - Scanner and Object

To get the user to enter data into your program, you would use the "Scanner" class. You will need to import this function first.

Code:

import java.util.Scanner;
public class Hello_World_1 {
   public static void main(String args[]){
       System.out.println("Enter three words: ");
       Scanner scan_ob = new Scanner(System.in); 
       cat = "Hello World again...";
       System.out.println(scan_ob.nextLine());
    }
}

Cmd Output:

Enter three words: apple bird chair
apple bird chair

.nextLine() is used to display everything that was entered into the keyboard up to the point you press Enter.

.next() is used to display single works in the input, upto the space. I think. I can't remember, but it's not important now.

Code:

import java.util.Scanner;
public class Hello_World_1 {
   public static void main(String args[]){
       System.out.println("Enter three words: ");
       Scanner scan_ob = new Scanner(System.in); 
       cat = "Hello World again...";
       System.out.println(scan_ob.next());
    }
}

Cmd Output:

Enter three words: apple bird chair
apple

Input Stores in Variable

The scanner object can be saved into a variable once the user has used the keyboard to enter some data.

Code:

import java.util.Scanner;
public class Hello_World_1 {
   public static void main(String args[]){
       Scanner scan_ob = new Scanner(System.in); 
       double fnum, snum, answer;
       System.out.println("Enter your first number: ");
       fnum = scan_ob.nextDouble();
       System.out.println("Enter your second number: ");
       snum = scan_ob.nextDouble();
       answer = fnum + snum;
       System.out.println(answer);
    }
}
// Variables of a similar data type can be set on the same line.
// Method that specifies the input data type.

Cmd Output:

Enter your first number: 12.3
Enter your first number: 3.2
15.5

Scanner Methods

Here is a table of all the different Scanner methods that can be used for data input.

Method Description
.nextBoolean() Reads a boolean value from the user
.nextByte() Reads a byte value from the user
.nextDouble() Reads a double value from the user
.nextFloat() Reads a float value from the user
.nextInt() Reads a int value from the user
.nextLine() Reads a String value from the user
.nextLong() Reads a long value from the user
.nextShort() Reads a short value from the user

Data Types in Java

Data types are divided into two groups:

  • Primitive data types - includes byte, short, int, long, float, double, boolean and char.
  • Non-primitive data types - such as String, Arrays and Classes.

The main difference between primitive and non-primitive data types are:

  1. Primitive types are predefined (already defined) in Java. Non-primitive types are created by the programmer and is not defined by Java (except for String).
  2. Non-primitive types can be used to call methods to perform certain operations, while primitive types cannot.
  3. A primitive type has always a value, while non-primitive types can be null.
  4. A primitive type starts with a lowercase letter, while non-primitive types starts with an uppercase letter.

A primitive data type specifies the size and type of variable values, and it has no additional methods.

There are eight primitive data types in Java.

Method Description Description
byte 1 bytes Stores whole numbers from -128 to 127
short 2 bytes Stores whole numbers from -32,768 to 32,767
int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647
long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits
boolean 1 bit Stores true or false values
char 2 bytes Stores a single character/letter or ASCII values

Constant

A constant is like a variable. It stores data, but the data does not change.

Code:

import java.util.Scanner;
import java.lang.Math;
class apple {
   public static void main(String args[]){
       System.out.println("Hello World");
       Scanner scan_ob = new Scanner(System.in); 
       final double pi = 3.1314159628;
       double radius, area;
       System.out.println("Enter Radius: ");
       radius = scan_ob.nextDouble();
       area = pi * (Math.pow(radius,2));
       System.out.println(area);
    }
}

Cmd Output:

Enter Radius:
20
1252.56638512

The final keyword is a non-access modifier used for classes, attributes and methods, which makes them non-changeable (impossible to inherit or override).

The final keyword is useful when you want a variable to always store the same value, like PI (3.14159...).

The final keyword is called a "modifier".


3